// pages/destdetail/destdetail.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    destdetail: {},
    px2rpx: 2,
    navOpacity: 0,
    statusNavBarHeight: 0,
    displayHeight: 0,
    isRisk: false
  },

  back: function () {
    wx.navigateBack({
      delta: 1,
    })
  },

  onPageScroll: function (e) {
    if (e.scrollTop <= 150) {
      this.setData({
        navOpacity: e.scrollTop / 150
      })
    } else {
      this.setData({
        navOpacity: 1
      })
    }
  },

  getTabBarHeight: function () {
    var that = this
    wx.getSystemInfo({
      success: function (res) {
        that.setData({
          px2rpx: 750 / res.windowWidth
        })
      }
    })
    // 获取状态栏高度
    let { model, statusBarHeight } = wx.getSystemInfoSync();
    // 得到右上角菜单的位置尺寸
    let { height, top } = wx.getMenuButtonBoundingClientRect();
    
    let isIOSRatio = model.match(/iPhone/) ? 3 : 2;
    // 计算状态栏与导航栏的总高度
    
    let navigationBarHeight = statusBarHeight + (top - statusBarHeight) * isIOSRatio +height;
    let displayHeight = (top - statusBarHeight) * isIOSRatio +height;
    this.setData({
      statusNavBarHeight: navigationBarHeight * this.data.px2rpx,
      displayHeight: displayHeight*this.data.px2rpx
    })
  },

  getCovidData: async function () {
    var app = getApp()
    if (app.globalData.covidData == null) {
      wx.showToast({
        title: '暂无疫情风险信息',
        duration: 2000,
        icon: 'error',
      })
      return []
    } else {
      console.log("信息存在")
      var highData = app.globalData.covidData.highlist
      var middleData = app.globalData.covidData.middlelist
      var fullData = highData.concat(middleData)
      return fullData
    }
  },

  isRisk: async function () {
    this.getCovidData().then(v => {
      console.log(v)
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (e) {
    this.getTabBarHeight()
    /* 根据传递过来的数据库表名和城市名称去数据库查询城市详情数据 */
    var that = this
    var city = e.city
    const db = wx.cloud.database()
    db.collection("cityInfo").where({
        nameEng: city
      })
      .get({
        success: res => {
          console.log(res)
          this.getCovidData().then(data => {
            for (var i = 0; i < data.length; i++) {
              if (res.data[0].fullName == data[i].city) {
                that.setData({
                  isRisk: true
                })
              }
            }
          })
          that.setData({
            destdetail: res.data[0]
          })
        },
      })
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {},

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})